# API作业-高德API
```python
#!/usr/bin/env python
# coding: utf-8

# # 1.地理编码

# In[ ]:


import requests


# In[6]:


geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"


# In[7]:


sxt_key = "de6216071f58b9c3b96bf20013f009e2"
address = "广东省广州市中山大学南方学院"
params = {
    "key":sxt_key,
    "address":address,
    "output":"json"
}
response = requests.get(url=geocode_url,params=params)


# In[8]:


response


# In[9]:


response.json()


# In[10]:


中大南方_location = response.json()['geocodes'][0]['location']
中大南方_location


# # 2.逆地理编码
# 

# In[11]:


def regeo(key,location,extensions="base",radius=None,poitype=None):
    """高德逆地理编码获取"""
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "extensions":extensions,
        "radius":radius,
        "poitype":poitype
    }
    r = requests.get(url=url,params=params)
    results = r.json()
    return results


# In[12]:


regeo(key=sxt_key,location=中大南方_location,radius=30000)


# In[13]:


url_regeo="https://restapi.amap.com/v3/geocode/regeo?parameters"
params={
    "key":sxt_key,
    "location":中大南方_location,
    "extensions":"all"
}
r=requests.get(url=url_regeo,params=params)


# In[14]:


r


# In[15]:


_roads_ = []
for item in r.json()['regeocode']['roads']:
#     print(item['name'])
    _roads_.append(item['name'])
_roads_


# In[16]:


r.json()


# # 3.路径规划

# 3.1  步行路径规划

# In[17]:


def walking(key,origin,location,sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':location,
        'sig':sig,
        'output':output,
        'callback':callback,
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[18]:


key='de6216071f58b9c3b96bf20013f009e2'
origin='113.678600,23.633329'
location='113.679177,23.632762'
walking(key,origin,location,sig=None,output='json',callback=None)


# In[19]:


a=walking(key,origin,location,sig=None,output='json',callback=None)['route']['paths'][0]['steps']
import pandas as pd
pd.json_normalize(a).instruction


# 3.2公交路径规划

# In[20]:


def transit(origin,location,city,cityid=None,extensions='base',strategy=0,nightflag=0,
            date=None,time=None,sig=None,output='json',callback=None):  
    url='https://restapi.amap.com/v3/direction/transit/integrated?parameters'  
    params={
        'key':key,
        'origin':origin,
        'destination':location,
        'city':city,
        'cityid':cityid,
        'extensions':extensions,
        'strategy':strategy,
        'nightflag':nightflag,
        'date':date,
        'time':time,
        'sig':sig,
        'output':output,
        'callback':callback          
    }    
    response=requests.get(url,params)    
    results=response.json()  
    return results


# In[21]:


origin='113.678600,23.633329'
location='113.668051,23.600869'
city='广州'
strategy='1'
transit(origin,location,city,cityid=None,extensions='base',strategy=strategy,nightflag=0,
            date=None,time=None,sig=None,output='json',callback=None)


# In[22]:


a=transit(origin,location,city,cityid=None,extensions='base',strategy=0,nightflag=0,
            date=None,time=None,sig=None,output='json',callback=None)['route']['transits']
import pandas as pd
pd.json_normalize(a)


# 3.3驾车路径规划

# In[23]:


def driving(key,origin,destination,extensions):
    #驾车路径规划
    url='https://restapi.amap.com/v3/direction/driving?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination,
        'extensions':extensions   
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[24]:


origin='113.678600,23.633329'
location='113.668051,23.600869'
extensions='base'
driving(key,origin,location,extensions)


# In[25]:


a=driving(key,origin,location,extensions)['route']['paths'][0]['steps']
pd.json_normalize(a)['instruction']


# 3.4骑行路径规划

# In[26]:


def bicycling(key,origin,destination):
    #骑车路径规划
    url='https://restapi.amap.com/v4/direction/bicycling?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[27]:


origin='113.678600,23.633329'
location='113.668051,23.600869'
bicycling(key,origin,location)


# In[28]:


a=bicycling(key,origin,location)['data']['paths'][0]['steps']
pd.json_normalize(a)['instruction']


# 3.5货车路径规划

# In[29]:


def truck(key,origin,location,size):
    #货车路径规划
    url='https://restapi.amap.com/v4/direction/truck?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':location, 
        'size':size
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[30]:


origin='113.678600,23.633329'
location='113.668051,23.600869'
size='2'
truck(key,origin,location,size)


# In[31]:


a=truck(key,origin,location,size)['data']['route']['paths'][0]['steps']
pd.json_normalize(a)['instruction']


# 3.5测量距离

# In[32]:


def distance(origins,location,type=''):
    #测量距离
    url='https://restapi.amap.com/v3/distance?parameters'
    key='de6216071f58b9c3b96bf20013f009e2'
    params={
        'key':key,
        'origins':origins,
        'destination':location, 
        'type':type
      
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[33]:


origins='113.678600,23.633329'
location='113.668051,23.600869'
type='0'
distance(origin,location)


# 4.行政区域查询

# In[42]:


def district(key,keywords=None,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json'):
    #行政区域查询
    url='https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'extensions':extensions,
        'page':page,
        'offset':offset,
        'filter':filter,
        'callback':callback,
        'output':output     
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[43]:


key='de6216071f58b9c3b96bf20013f009e2'
keywords='广西'
subdistrict='1'
district(key,keywords,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json')


# 找出各个城市名字

# In[44]:


a=district(key,keywords,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json')['districts'][0]['districts']
import pandas as pd
pd.json_normalize(a)['name']


# # 5.搜索POI

# 5.1关键词搜索

# In[45]:


def text(keywords,city='',extensions='',citylimit=''):
    #关键词搜索
    url='https://restapi.amap.com/v3/place/text?parameters'
    key='18ef7dd6aeefc21dade574262c00f213'
    params={
        'key':key,
        'keywords':keywords,
        'city':city,
        'extensions':extensions,
        'citylimit':citylimit
      
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[46]:


keywords='美食'
city='广西'
citylimit='true'
text(keywords,city,extensions,citylimit)


# 5.2周边搜索

# In[47]:


def around(location,keywords=''):
    #周边搜索
    url='https://restapi.amap.com/v3/place/around?parameters '
    key='de6216071f58b9c3b96bf20013f009e2'
    params={
        'key':key,
        'keywords':keywords,
        'location':location
      
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[48]:


keywords='电影'
location='108.321759,22.816652'
around(location,keywords)


# 5.3多边形搜索

# In[49]:


def poly(key,polygon,keywords=None,types=None,offset='20',page='1',extensions='base',sig=None,output='json',callback=None):
    #多边形搜索
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    params={
        'key':key,
        'polygon':polygon,
        'keywords':keywords,
        'types':types,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback     
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[50]:


polygon='108.323985,22.813839|108.321096,22.815424|108.321510,22.815693'
poly(key,polygon,keywords=None,types=None,offset='20',page='1',extensions='base',sig=None,output='json',callback=None)


# 5.4ID查询

# In[51]:


def detail(id):
    #id查询搜索
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    key='de6216071f58b9c3b96bf20013f009e2'
    params={
        'key':key,
        'id':id
      
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[52]:


id=''
detail(id)


# # 6. IP定位

# In[53]:


def ip(key,id):
    # ip定位
    url='https://restapi.amap.com/v3/ip?parameters'
    key='de6216071f58b9c3b96bf20013f009e2'
    params={
        'key':key,
        'id':id
      
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[54]:


id='B0GUZ9HNDS'
ip(key,id)


# # 7.静态地图

# In[55]:


def amap(key,location,zoom,size='400*400',scale='1',markers=None,labels=None,paths=None,traffic='0',sig=None):
    #获取静态地图信息
    url='https://restapi.amap.com/v3/staticmap?parameters'
    params={
    'key':key,
    'location':location,
    'zoom':zoom,
    'scale':scale,
    'markers':markers,
    'labels':labels,
    'paths':paths,
    'traffic':traffic,
    'sig':sig
    }
    r=requests.get(url,params)
    data=r.url
    return data


# In[56]:


location='113.677460,23.630490'
zoom='17'
amap(key,location,zoom,size='400*400',scale='1',markers=None,labels=None,paths=None,traffic=None,sig=None)


# # 8.地标转换

# In[57]:


def convert(key,locations,coordays='',sig='',output=''):
    url='https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params={
        'key':key,
        "locations":locations,
        'coordays':coordays,
        'sig':sig,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[58]:


locations='116.481499,39.990475|116.481499,39.990375'
convert(key,locations,coordays='',sig='',output='')


# # 9.天气查询

# In[61]:


def weather(key,city,extensions='base',output=''):
    # 天气查询
    url='https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    params={
        'key':key,
       'city':city,
        'extensions':extensions,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[62]:


city='440117'
weather(key,city,extensions='base',output='')


# # 10.输入提示

# In[63]:


def input(key,keywords,type=None,location=None,city=None,
          citylimit='false',datatype=None,sig=None,output='json',callback=None):
    # 输入提示
    url='https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'type':type,
        'location':location,
        'city':city,
        'citylimit':citylimit,
        'datatype':datatype,
        'sig':sig,
        'output':output,
        'callback':callback
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[64]:


keywords='从化客运站'
input(key,keywords,type=None,location=None,city=None,citylimit='false',datatype=None,sig=None,output='json',callback=None)


# # 11.交通态势

# In[65]:


def traffic(key,rectangle,level='5',extensions='base',sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/traffic/status/rectangle?parameters'
    params={
        'key':key,
        'rectangle':rectangle,
        'level':level,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback,
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[66]:


rectangle='113.594203,23.531854;113.599374,23.528771'
traffic(key,rectangle,level='5',extensions='base',sig=None,output='json',callback=None)


# In[ ]:





